//
//  YRComposeToolBar.swift
//  新浪微博
//
//  Created by Viper on 16/7/3.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
enum YRComposeToolBarType : Int {
    case Picture  = 10
    case Mention  = 20
    case Trend  = 30
    case Emoticon  = 40
    case Add  = 50
}
class YRComposeToolBar: UIView {
    //切换键盘按钮
    var emoticonButton : UIButton?
    
    var isEmotion : Bool = false{
        
        didSet{
            //如果isEmotion == true 代表是表情键盘  button显示键盘 如果为false 代表显示的是系统键盘 button显示为笑脸
            
            if isEmotion == true {
                //显示键盘
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)

            }else{
                //显示笑脸
                
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
                
            }
            
        }
    }
    
    var closure:((type : YRComposeToolBarType )->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
       //添加控件
       let pictureButton = addChildButton("compose_toolbar_picture",type:.Picture)
       let mentionButton = addChildButton("compose_mentionbutton_background",type:.Mention)
       let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
       emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
       let addButton = addChildButton("compose_add_background",type: .Add)

        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            
            make.top.leading.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
            
        }
    }
    //创建button按钮的公共方法
    func addChildButton(imageNamed:String , type : YRComposeToolBarType)->UIButton{
        
        let button = UIButton()
        button.tag = type.rawValue
        button.setImage(UIImage(named:imageNamed), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"\(imageNamed)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        button.addTarget(self, action:#selector(YRComposeToolBar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        addSubview(button)
        return button
    }
    
    func buttonClick(button : UIButton){
        
//        print("----")
        closure?(type:YRComposeToolBarType(rawValue: button.tag)!)
    }
    

}
